package com.api.gateway.service.impl;

import com.api.gateway.exception.ArgumentCountException;
import com.api.gateway.service.ValidationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by hao.g on 18/4/2.
 */
@Service
public class ValidationServiceImpl implements ValidationService {

    @Value("${api.param.count}")
    private int paramsCount;

    @Override
    public void check(String[] params) throws ArgumentCountException {
        if (params.length < paramsCount){
            throw new ArgumentCountException("api参数长度有误");
        }

        if (!params[0].startsWith("api=")){
            throw new ArgumentCountException("wrong api " + params[0]);
        }else {
            String param = params[0];
            params[0] = param.substring(param.indexOf("=") + 1);
        }

        if (!params[1].startsWith("data=")){
            throw new ArgumentCountException("wrong data " + params[1]);
        }else {
            String param = params[1];
            params[1] = param.substring(param.indexOf("=") + 1);
        }

        if (!params[2].startsWith("charset=")){
            throw new ArgumentCountException("wrong charset " + params[2]);
        }else {
            String param = params[2];
            params[2] = param.substring(param.indexOf("=") + 1);
        }

        if (!params[3].startsWith("encKey=")){
            throw new ArgumentCountException("wrong encKey " + params[3]);
        }else {
            String param = params[3];
            params[3] = param.substring(param.indexOf("=") + 1);
        }

        if (!params[4].startsWith("sign=")){
            throw new ArgumentCountException("wrong sign " + params[4]);
        }else {
            String param = params[4];
            params[4] = param.substring(param.indexOf("=") + 1);
        }

        if (!params[5].startsWith("timestamp=")){
            throw new ArgumentCountException("wrong timestamp " + params[5]);
        }else {
            String param = params[5];
            params[5] = param.substring(param.indexOf("=") + 1);
        }

        if (!params[6].startsWith("version=")){
            throw new ArgumentCountException("wrong version " + params[6]);
        }else {
            String param = params[6];
            params[6] = param.substring(param.indexOf("=") + 1);
        }
    }
}
